package com.diors.game.engine.arpg.aoi.tree4;

import java.util.List;


/**
 * 4叉树工具类
 * @author	darren.ouyang <ouyang.darren@gmail.com>
 * @date	2013年12月6日
 * @version 1.0
 * @copyright Copyright (c) 2013, darren.ouyang
 */
public class Tree4Utils 
{
	private final static Tree4Utils instance = new Tree4Utils();
	
	private Tree4Utils (){}
	
	public static Tree4Utils getInstance ()
	{
		return instance;
	}
	
	/**
	 * 寻找节点
	 * @param tree
	 * @param x
	 * @param y
	 * @return
	 * @author	darren.ouyang
	 * @create	2013年12月6日 下午3:26:55
	 */
	public PointEntity getPoint (Tree4Point tree, int x, int y)
	{
		Tree4Point nodeTree = getNodeTree(tree, x, y);
		if (nodeTree == null)
			return null;
		else 
			return nodeTree.getPoint();
	}
	
	/**
	 * 寻找节点树
	 * @param tree
	 * @param x
	 * @param y
	 * @return
	 * @author	darren.ouyang
	 * @create	2013年12月6日 下午3:26:55
	 */
	private Tree4Point getNodeTree (Tree4Point tree, int x, int y)
	{
		if (tree == null)
			return null;
			
		int treeX = tree.getX();
		int treeY = tree.getY();
		
		if (treeX == x && treeY == y)
			return tree;
		
		if (x > treeX)
		{
			if (y > treeY)
				return getNodeTree(tree.getNortheast(), x, y);
			else
				return getNodeTree(tree.getSoutheast(), x, y);
		}
		else
		{
			if (y > treeY)
				return getNodeTree(tree.getNorthwest(), x, y);
			else
				return getNodeTree(tree.getSouthwest(), x, y);
		}
	}
	
	public List<PointEntity> getPoints (Tree4Point tree, int x, int y, int r)
	{
		Tree4Point nodeTree = getNodeTree(tree, x, y);
		Tree4Point parentTree = nodeTree.getParentTree();
		int parentX = parentTree.getX();
		int parentY = parentTree.getY();
		
		
		
		return null;
	}
}
